package servlets;


import bean.Result;
import com.alibaba.fastjson.JSON;
import entity.User;
import service.UserService;
import service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/register")
public class registServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String name=req.getParameter("name");
        String psw=req.getParameter("psw");
        UserService us=new UserServiceImpl();//设置相应内容
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter pw=resp.getWriter();

//        List<User> u=us.queryUser(name);
//        if(u!=null){
//            pw.write(JSON.toJSONString(new Result(201,"用户名已存在")));
//        }else{
            boolean boo=us.insertUser(name, psw);
            if(boo){
                //插入成功
                pw.write(JSON.toJSONString(new Result(200,"注册成功")));
            }else {
                pw.write(JSON.toJSONString(new Result(200,"注册失败")));
            }
//        }
    }
}
